package 力扣题库;

public class p695岛屿的最大面积 {
    public int maxAreaOfIsland(int[][] grid) {
        int res=0;

        int row=grid.length;
        int column=grid[0].length;
        for (int i = 0; i <row ; i++) {
            for (int j = 0; j <column ; j++) {
                if (grid[i][j]==1) {
                    //当前位置是岛屿，且还没有被检查过，那么对该位置开始进行 深度优先遍历
                    res=Math.max(res,backTrack(grid,i,j));
                }
            }
        }
        return res;
    }

    private int backTrack(int[][] grid, int x, int y) {
        //递归出口
        if (x<0 || x>=grid.length || y<0 || y>=grid[0].length) {
            //超出矩阵，返回0
            return 0;
        }

        if (grid[x][y]!=1) {
            //当前位置已经检查过 或者 是水域，返回0
            return 0;
        }
        grid[x][y]=2;

        int area=1;
        area += backTrack(grid,x+1,y);
        area += backTrack(grid,x-1,y);
        area += backTrack(grid,x,y-1);
        area += backTrack(grid,x,y+1);
        return area;
    }
}
